package com.util.concurrent;

import org.junit.Test;

import java.time.LocalDateTime;
import java.util.concurrent.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-25 16:50:28
 */
public class ThreadPoolTest {

    @Test
    public void test2() throws InterruptedException {
        LinkedBlockingQueue<Callable<?>> delayQueue = new LinkedBlockingQueue<>();
        LinkedBlockingQueue<Callable<?>> interactionQueue = new LinkedBlockingQueue<>();
        LinkedBlockingQueue<Callable<?>> microQueue = new LinkedBlockingQueue<>();
        RenderMain main = new RenderMain(
                delayQueue,
                interactionQueue,
                microQueue
        );
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);
        pool.scheduleWithFixedDelay(() -> {
            int i = ThreadLocalRandom.current().nextInt(5);
            switch (i) {
                case 0:
                    delayQueue.add(() -> LocalDateTime.now() + "delay" + i);
                    break;
                case 1:
                    interactionQueue.add(() -> LocalDateTime.now() + "interaction" + i);
                    break;
                case 2:
                    microQueue.add(() -> LocalDateTime.now() + "micro" + i);
                    break;
                default:
                    delayQueue.add(() -> LocalDateTime.now() + "delay" + i);
                    microQueue.add(() -> LocalDateTime.now() + "micro" + i);
                    interactionQueue.add(() -> LocalDateTime.now() + "interaction" + i);
                    break;
            }
        }, 1, 100, TimeUnit.MILLISECONDS);

        while (true) {
            Object render = main.render();

            System.out.println("render = " + render);
            TimeUnit.SECONDS.sleep(3);
            System.out.println("------------------------------------------------");
        }
    }

    @Test
    public void test1() {

    }

    private static final class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
        public RejectedExecutionHandlerImpl() {
        }

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        }

    }
}
